Public Class FormFamilyAED
    Public empId, fId As Int16
    Dim emp As New EmployeeInfo
    Public family As New FamilyInfo

    Private Sub FormFamilyAED_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub
    Public Sub ClearText()

        For Each Clear As Control In GroupBox1.Controls
            If TypeOf Clear Is TextBox Then
                Clear.Text = ""
            End If
        Next
    End Sub

    Private Sub LoadName()
        For Each obj As Object In GetEmployeeName.GetEmployee.Values
            cboEmp_Name.Items.Add(obj)
        Next
    End Sub

    Private Sub cboEmp_Name_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboEmp_Name.SelectedIndexChanged
        Dim group As EmployeeInfo = CType(cboEmp_Name.SelectedItem, EmployeeInfo)
        empId = group.EmployeeId
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        ClearText()
        Dbinterface.OpenConnection()
        LoadName()

    End Sub

    Private Sub btnSaveUpdate_Click(sender As Object, e As EventArgs) Handles btnSaveUpdate.Click
        SaveUpdate()
    End Sub
    Private Sub SaveUpdate()
        Try
            'Passing values from object in class to TextBox
            With family
                .FamilyId = fId
                .EmployeeId = empId
                .RelativeName = txtRelative.Text
                .Dob = txtDOB.Text
                .Occupation = txtOccupation.Text
                .Note = txtNote.Text
                .Phone = txtPhone.Text
                .Address = txtAddress.Text
                .RelativeType = txtRelativeType.Text
                .Email = txtEmail.Text
                .Active = True

                If fId = Nothing Then
                    'Insert familycation

                    Families.AddFamily(family)
                Else
                    'Update familycation

                    Families.UpdateFamily(family)

                End If

            End With
            AddFamilyToDataGridView()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Me.Close()
    End Sub
    Public Sub AddFamilyToDataGridView()
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If
            'Passing vlues DataGridView in form familycation
            With FormFamilies.dgvFamilies.CurrentRow
                .Cells("EMPLOYEE_NAME").Value = cboEmp_Name.Text
                .Cells("RELATIVE_NAME").Value = family.RelativeName
                .Cells("DOB").Value = family.Dob
                .Cells("OCCUPATION").Value = family.Occupation
                .Cells("NOTE").Value = family.Note
                .Cells("PHONE").Value = family.Phone
                .Cells("ADDRESS").Value = family.Address
                .Cells("RELATIVE_TYPE").Value = family.RelativeType
                .Cells("EMAIL").Value = family.Email
                If fId = Nothing Then

                    .Cells("FAMILY_ID").Value = family.FamilyId
                    RefreshData.DescendingRefresh(FormFamilies.dgvFamilies, 0)

                    FormFamilies.dgvFamilies.CurrentCell = FormFamilies.dgvFamilies.Rows(0).Cells(1)
                Else

                    FormFamilies.dgvFamilies.CurrentCell = FormFamilies.dgvFamilies.Rows(0).Cells(1)

                    Exit Sub
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
    End Sub
End Class